/**
 * Created by Administrator on 2017/7/20.
 */
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const uglifyJsPlugin = webpack.optimize.UglifyJsPlugin;

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

//打包node_modules下的模块
const nodeModules = {};
// 不打包node_modules下的模块
// fs.readdirSync('node_modules')
//   .filter(function (x) {
//     return ['.bin'].indexOf(x) === -1;
//   })
//   .forEach(function (mod) {
//     nodeModules[mod] = 'commonjs ' + mod;
//   });

module.exports = {
  entry: "./index.js",
  output: {
    filename: "./dist/index.js"
  },
  target: 'node',//解决打包fs，path等node原生模块的问题
  resolve: {
    extensions: ['.js', '.json'],
    alias: {
      '@': resolve('src')//加载路径问题
    }
  },
  externals: nodeModules,
  module: {
    loaders: [//使用es6语法
      {
        test: /\.js[x]?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015']
        }
      },
    ]
  },
  plugins: [//压缩代码
    new uglifyJsPlugin({
      compress: {
        warnings: false
      }
    })
  ]
}